/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alarmlistener.logger.logGui.elements;

import alarmlistener.elements.Input;
import alarmlistener.logger.InputLogger;
import alarmlistener.logger.logevent.InputEvent;
import alarmlistener.logger.logevent.InputListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author rbs
 */
public class InputTable extends JPanel{

       DefaultTableModel dtm;
       
       InputLogger logger;
               
    public InputTable() {
        setLayout(new BorderLayout());
        
        this.logger = InputLogger.getInstance();
              
        logger.addInputListener(new InputListener() {
            @Override
            public void onEvent(InputEvent event) {
               addRow(event.getInput());
            }
        });
        
        String rowNames[];
        
        rowNames = new String[]{"Datum", "Zeit", "RIC", "Funktion", "Text", "relevant"};
        
        dtm = new DefaultTableModel(rowNames, 0);
        
        getLastInputs();
        
        JTable table = new JTable(dtm);
        
       table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table,
                    Object value, boolean isSelected, boolean hasFocus,
                    int row, int column) {
 
                Component c = super.getTableCellRendererComponent(table, value,
                        isSelected, hasFocus, row, column);
 
                if (table.getValueAt(row, 4).toString().startsWith("-->"))
                {
                    setBackground(Color.YELLOW);
                    if (column == 5)
                        setValue("");
                }
                else if (table.getValueAt(row, 5).equals("true")) {
                    setBackground(Color.GREEN);
                } else {
                    setBackground(Color.WHITE);
                }
                return this;
            }
       });
        
        table.getColumnModel().getColumn(0).setMinWidth(70);
        table.getColumnModel().getColumn(1).setMinWidth(70);
        table.getColumnModel().getColumn(2).setMinWidth(70);
        table.getColumnModel().getColumn(3).setMinWidth(50);
        table.getColumnModel().getColumn(4).setMinWidth(100);
        table.getColumnModel().getColumn(5).setMinWidth(80);

        table.getColumnModel().getColumn(0).setMaxWidth(70);
        table.getColumnModel().getColumn(1).setMaxWidth(70);
        table.getColumnModel().getColumn(2).setMaxWidth(70);
        table.getColumnModel().getColumn(3).setMaxWidth(50);
        table.getColumnModel().getColumn(5).setMaxWidth(80);
     
        
        
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        //setPreferredSize(table.getPreferredSize());
        add(new JScrollPane(table), BorderLayout.CENTER);
        
    }
    
    
    private void getLastInputs()
    {
        dtm.setRowCount(0);
        
        ArrayList<Input> fmsInputs;
        fmsInputs = logger.getInputs();
                    
        for (Input input: fmsInputs)
        {
            addRow(input);
        }
    }
    
    private void addRow(Input input)
    {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(input.date);

        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("dd.MM.yyyy");
        java.text.SimpleDateFormat timeFormat = new java.text.SimpleDateFormat("HH:mm:ss");
        String date = dateFormat.format(input.date);
        String time = timeFormat.format(input.date);

        String rowData[] = {date, time, input.fms1, input.fms2, input.fms3, input.relevant.toString()};
        dtm.addRow(rowData);
    }
    
}
